<template>
  <div class="loginBox">
    <el-form status-icon :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="80px" class="loginRuleForm">
      <div class="login_img-box">
        <img class="logo_img" src="@/assets/logo.png" alt="">
      </div>
      <el-form-item label="用户名" prop="fUserAccount">
        <el-input type="text" autocomplete="off" v-model="ruleForm.fUserAccount"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="fUserPassword">
        <el-input type="password" autocomplete="off" v-model="ruleForm.fUserPassword"></el-input>
      </el-form-item>
      <el-form-item class="loagin_btn">
        <el-button type="primary" @click="handlelogin">登录</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import jsCookie from 'js-cookie'
import { User } from '@/api/apiConfig'

export default defineComponent({
  setup() {
    const ruleFormRef = ref()
    const store = useStore()
    const authList = ref<any[]>([])
    let ruleForm = reactive<{ fUserAccount: string; fUserPassword: string }>({
      fUserAccount: '',
      fUserPassword: '',
    })
    const rules = reactive({
      fUserAccount: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
      ],
      fUserPassword: [
        { required: true, message: '请输入密码', trigger: 'blur' },
      ],
    })
    const router = useRouter()
    const handlelogin = () => {
      ruleFormRef.value.validate((valid: any) => {
        if (valid) {
          const params = {
            fUserAccount: ruleForm.fUserAccount,
            fUserPassword: ruleForm.fUserPassword,
          }
          User.login(params).then((res: any) => {
            ElMessage.success({
              message: '登录成功',
              type: 'success',
              onClose: () => {
                jsCookie.set('token', res.data.token)
                jsCookie.set('userInfo', JSON.stringify(res.data.userIfo))
                authList.value = res.data.authList.unshift({
                  f_id: '1',
                  f_name: '首页',
                  f_url: '',
                  f_sort: '1',
                  f_pid: '0',
                  f_type: '0',
                  f_icon: 'el-icon-s-home',
                  children: [
                    {
                      f_id: '2',
                      f_name: '首页',
                      f_url: '/welcome',
                      f_sort: '1',
                      f_pid: '0',
                      f_type: '0',
                      f_icon: '',
                    },
                  ],
                })
                store.commit('SET_RIGHTLIST', res.data.authList)
                router.push({
                  path: '/home',
                })
              },
            })
          })
        } else {
          return false
        }
      })
    }
    const resetForm = () => {
      ruleFormRef.value.resetFields()
    }
    return {
      ruleForm,
      handlelogin,
      rules,
      ruleFormRef,
      resetForm,
    }
  },
})
</script>


<style scoped lang="scss">
@import '../../assets/common';
.loginBox {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: $color-274a6c;
  .loginRuleForm {
    background: $color-fff;
    border-radius: 15px;
  }
  .login_img-box {
    position: absolute;
    top: -65px;
    left: 300px;
  }
  .logo_img {
    width: 135px;
    height: 125px;
    border-radius: 100%;
    box-shadow: 0 0 10px $color-999;
    background: $color-fff;
  }
  .loginRuleForm {
    position: relative;
    padding: 100px 50px;
    width: 600px;
    border: $color-fff;
  }
  .loagin_btn {
    width: 90%;
    text-align: center;
  }
}
</style>